﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Server.API.Application.Command.DeepProcess;
using Server.API.DTOs;

namespace Server.API.Application.Handler.DeepProcess
{
    public class GetDeepProcessHandler:IRequestHandler <GetDeepProcessCommand, APIResult<DeepProcessDTO>>
    {
        private readonly IRepository<DeepProcessModel> deepProcessway;
        private readonly IMapper mapper;
        private readonly ILogger<GetDeepProcessHandler> log;

        public GetDeepProcessHandler(IRepository<DeepProcessModel> deepProcessway, IMapper mapper,ILogger<GetDeepProcessHandler> log)
        {
            this.deepProcessway = deepProcessway;
            this.mapper = mapper;
            this.log = log;
        }
        /// <summary>
        /// 获取深加工详情
        /// </summary>
        /// <param name="request">深加工主键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>深加工信息</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<DeepProcessDTO>> Handle(GetDeepProcessCommand request, CancellationToken cancellationToken)
        {
            APIResult<DeepProcessDTO> res=new APIResult<DeepProcessDTO>();
            var model=await deepProcessway.GetValue(request.DeepProcessId);
            var value=mapper.Map<DeepProcessDTO>(model);
            if(value==null)
            {
                log.LogInformation("不存在此深加工");
                res.Num=StateCode.Fail;
                res.Message="不存在此深加工";
            }
            else
            {
                log.LogInformation("获取成功");
                res.Data=value;
                res.Num=StateCode.OK;
                res.Message="获取成功";
            }
             return res;
        }
    }
}
